let Utils = require('Utils')

window.Global = {
    eventNode: null,
}

var Game = cc.Class({
    extends: cc.Component,
    properties: {
        sock: cc.Node,
        loading: cc.Node,
        setting: cc.Node
    },


    onLoad() {
        window.sock = this.sock.getComponent("TxSocket")
        window.sock.handle = this
        this.registerEvent()
        /**
         * sid 用户id
         * isd 是否是主播
         * roomId 房间号
         */
        let {sid, st, gt, isd, name, roomId, avatar, goldLeastCount} = Utils.parseQueryString(window.location.search)
        if (!sid || !isd || !roomId) {
            return
        }
        window.user = {
            header: avatar,
            uid: parseInt(sid),
            name: name,
            roomId: roomId,
            isd: isd === "false" ? false : true
        }
        if (window.user.isd) {
            this.setting.active = true
        }
    },

    registerEvent: function () {
        window.ui = {}
        var callAll = function (rootNode) {
            if (rootNode.name != "Misco") {          //防止循环递归，导致栈溢出
                for (let i = 0; i < rootNode._components.length; ++i) {
                    if ((rootNode._components[i].registerEvent != undefined && typeof (rootNode._components[i].registerEvent) == "function")) {
                        rootNode._components[i].registerEvent()
                    }
                }
            }
            let children = rootNode.children;
            for (let i = 0; i < children.length; ++i) {
                callAll(children[i])
            }
        }

        var root = cc.find("Canvas")
        callAll(root)

        /*网关消息*/
        window.sock.node.on("4-1", (e) => {
            window.sock.connect(`ws://${e.detail.fVo.ip}:${e.detail.fVo.port}`)
        })
        /*用户登录消息*/
        window.sock.node.on("7-1", (e) => {
        })

        window.sock.node.on("11-2", e => {
            window.room = e.detail.roomVo
        })

        // window.sock.node.emit("11-4");
        //window.sock.node.emit("11-23");
    },

    open() {
        window.sock.gateway({uid: 2})
    },

    beforeSend() {
        //this.loading.active = true
    },

    finishRecvMessage() {
        //this.loading.active = false
    },

    didConnectServer() {
        window.sock.login({type: 1, uid: window.user.uid})
    },

    gameUid() {
        return 1
    },
    /**
     * 创建房间
     * @param {any} data
     */
    roomConfig(data) {
    },

    /**
     * 游戏状态改变
     * @param {any} state
     * @param {number} [countDown=0]
     */
    gameStateChanged(state, countDown = 0) {
    },

    /**
     * 开始游戏
     */
    playing(e) {
    },

    /**
     * 加入房间
     */
    join() {
    },

    /**
     * 坐下
     */
    sit() {
    },

    /**
     * 玩家头像点击
     */
    kickOrStand(id) {
    },

    /**
     * 站起
     */
    stand() {
    },

    /**
     * 加倍
     * @param {any} x 倍数
     */
    redouble(x, y) {
    },

    /**
     * 发送公共押注信
     * @param {any} betInfo
     */
    sendPublicBetInfo(betInfo) {
        if (!Global.state.isSited && Global.user._id !== Global.room.dealerId) {
            console.log('send' + JSON.stringify(betInfo))
            this.send('area.areaHandler.betGuest', {opt: betInfo, areaId: Global.room.id}, {
                success: data => {
                    if (data.length) {
                        for (let item of data) {
                            let {betType, betMultiTotal} = item
                            this.table.publicBet({type: betType, myBet: betMultiTotal})
                        }
                    }
                }
            })
        }
    },

    /**
     * 继续玩游戏
     */
    playAgain() {
        if (!this.checkMoney({betBaseMoney: Global.room.baseBetMoney})) {
            this.stand()
        }
    },

    giveUp() {
        this.stand()
    },

    checkMoney(info) {
        let {betBaseMoney} = info
        if (betBaseMoney > Global.user.gold) {
            this.ui.toastMsg('金币余额不足')
            return false
        }
        return true
    },

    testJoin() {
        window.sock.standUp({index: 1})
        window.sock.join({ roomId: window.user.roomId });
    }
});
